<!-- eslint-disable eqeqeq -->
<!-- eslint-disable eqeqeq -->
<template>
  <div class="app-container">
    <el-form
      v-show="showSearch"
      ref="queryForm"
      :model="queryParams"
      size="small"
      :inline="true"
      @submit.native.prevent
    >
      <el-form-item label="公共字典名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入公共字典名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增 </el-button>
      </el-col>
    </el-row>
    <hk-table
      :loading="loading"
      :table-data="dictList"
      :table-columns="tableList"
      :operate="true"
      :total="total"
      :show-index="true"
      :query-params="queryParams"
      @handleSelectionChange="handleSelectionChange"
      @changePage="getList"
    >
      <template #code="scope">
        <el-button
          type="text"
          @click="$router.push({ path: prefix+'/mlk/dictDataList/' + scope.row.id, query: { itemType: scope.row.code } })"
        >{{ scope.row.code }}</el-button>
      </template>
      <template #operate="scope">
        <el-button size="mini" type="text" @click="handleUpdate(scope.row)">修改 </el-button>
        <el-button size="mini" type="text" @click="handleDelete(scope.row)">删除 </el-button>
      </template>
    </hk-table>

    <AddDictClassify ref="editDialogRef" :form="form" :title="title" @handleSubmit="getList" />
  </div>
</template>

<script>
import { dictClassify, delDictClassify, queryClassify } from '@/api/milkyway/dictManager'
import HkTable from '@/components/Hk-Table'
import AddDictClassify from './addDictClassify.vue'

export default {
  name: 'MlkCommonDict',
  components: { HkTable, AddDictClassify },
  data() {
    return {
      prefix: window.prefix,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      dictList: [],
      // 弹出层标题
      title: '',
      // 查询参数
      queryParams: {
        current: 1,
        size: 10
      },
      // 表单参数
      form: {},

      //  表格
      tableList: [
        { label: '字典名称', param: 'name' },
        { label: '字典编码', param: 'code', type: 'slot' }
      ]
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询列表 */
    getList(val) {
      this.loading = true
      this.queryParams = { ...this.queryParams, ...val, scrollToTop: true }
      dictClassify(this.queryParams).then((res) => {
        this.dictList = res.list
        this.total = res.total
        this.loading = false
        this.queryParams.scrollToTop = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.form = {}
      this.$refs.editDialogRef.show()
      this.title = '添加字典分类'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.title = '更新字典分类'
      const id = row.id
      queryClassify(id).then((res) => {
        this.form = res
      })
      this.$refs.editDialogRef.show()
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id
      this.$modal
        .confirm('是否确认所选项数据项？')
        .then(function() {
          return delDictClassify(id)
        })
        .then(() => {
          this.$modal.msgSuccess('删除成功')
          this.getList()
        })
        .catch((error) => {
          console.log(error)
        })
    }
  }
}
</script>
<style lang="scss" scoped>
.app-container {
  height: 100%;
  display: flex;
  flex-direction: column;
}
.targetTable {
  flex: 1;
  overflow: hidden;
}
</style>
